package com.nanyang.tms.manager.shipment.impl;

import cn.hutool.json.JSONUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.sys.api.dto.response.personal.PersonalDetailsResponse;
import com.nanyang.sys.api.service.personal.PersonalRpcService;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.dict.TmsExceptionEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.RpcCheckUtils;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.shipment.ShipmentBillDao;
import com.nanyang.tms.dao.shipment.ShipmentRelationDao;
import com.nanyang.tms.domain.shipment.ShipmentBillDO;
import com.nanyang.tms.domain.shipment.ShipmentBillQuery;
import com.nanyang.tms.domain.shipment.ShipmentBillVO;
import com.nanyang.tms.manager.shipment.ShipmentBillManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Component("shipmentBillManager")
public class ShipmentBillManagerImpl implements ShipmentBillManager {
    @Autowired
    private ShipmentBillDao shipmentBillDao;
    @Autowired
    private ShipmentRelationDao shipmentRelationDao;
    @Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;
    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private PersonalRpcService personalRpcService;
    @Override
    public PageBean<ShipmentBillVO> listShipmentBill(ShipmentBillQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = shipmentBillDao.countShipmentBill(query);
            PageBean<ShipmentBillVO> page = new PageTableBean<ShipmentBillVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(shipmentBillDao.listShipmentBill(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @SystemLog(module =SystemLog.Module.WAYBILL,template="shipmentBillAdd")
    @Override
    public void saveShipmentBill(ShipmentBillDO shipmentDO) {
        try {
            shipmentDO.setShipmentCode(TmsSerializeConstant.SHIPMENTBILL_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SHIPMENTBILL_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY));
            ApiResult<List<PersonalDetailsResponse>> listApiResult = personalRpcService.queryPersonalInfoList(Arrays.asList(shipmentDO.getGmtCreatePin()));
            if(RpcCheckUtils.isRpcInvokeError(listApiResult)){
                log.error("查询用户信息失败：入参{},返回{}", shipmentDO.getGmtCreatePin(),JSONUtil.toJsonStr(listApiResult));
                throw new TmsManagerException(TmsExceptionEnum.SELECT_USER_ERROR.getCode(),TmsExceptionEnum.SELECT_USER_ERROR.getMessage());
            }
            shipmentDO.setOperator(listApiResult.getData().get(0).getEmployeeName());
            shipmentBillDao.insert(shipmentDO);
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @SystemLog(module =SystemLog.Module.WAYBILL,template="shipmentBillCancel")
    @Override
    public void cancelState(ShipmentBillDO shipmentDO) {
        try {
            int i = shipmentRelationDao.countByShipmentCode(shipmentDO.getShipmentCode());
            if(i>0){
                throw new TmsManagerException(TmsExceptionEnum.RECEIPTBILL_CANCEL_FAIL.getCode(),TmsExceptionEnum.RECEIPTBILL_CANCEL_FAIL.getMessage());
            }
            shipmentBillDao.cancelState(shipmentDO);
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
}
